package com.monkeygroup.mygoplus.view.home.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.view.home.activity.rent.PublishedRentActivity;
import com.monkeygroup.mygoplus.view.home.activity.rent.PublishedRentLandAcitivity;
import com.monkeygroup.mygoplus.view.home.activity.rent.PublishedRentOfficeActivity;
import com.monkeygroup.mygoplus.view.home.activity.rent.PublishedRentShopAcitivity;
import com.monkeygroup.mygoplus.view.home.activity.sell.PublishedActivity;
import com.monkeygroup.mygoplus.view.home.activity.sell.PublishedLandActivity;
import com.monkeygroup.mygoplus.view.home.activity.sell.PublishedShopActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/5/22.
 */

public class PublishedSecondActivity extends BaseActivity implements View.OnClickListener {

    public static void gotoActivity(Activity activity,String propertytype,String type) {
        Intent intent = new Intent(activity,
                PublishedSecondActivity.class);

        intent.putExtra("type", type);
        intent.putExtra("propertytype", propertytype);

        activity.startActivity(intent);
    }

    private ListView publishList;
    private List<String> leftListItems;

    private LeftAdapter adapter;

    private String type;
    private String propertytype;
    private ImageView imgBack;
    private TextView txtTitle;
    private LinearLayout layout_shortcut;


    @Override
    public void onClick(View view) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.published_second);
    }

    @Override
    public void initView() {

        publishList= (ListView)findViewById(R.id.publish_list);

        leftListItems = new ArrayList<String>();

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        layout_shortcut= (LinearLayout) findViewById(R.id.layout_shortcut);
        layout_shortcut.setVisibility(View.INVISIBLE);


         adapter = new LeftAdapter(PublishedSecondActivity.this);
        publishList.setAdapter(adapter);

        Intent intent = this.getIntent();
        Bundle bundle =  intent.getExtras();
        type = bundle.getString("type");
        propertytype= bundle.getString("propertytype");

        fillData(type);

    }

    @Override
    public void setListener() {


    }

    @Override
    public void doEvent() {
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void fillData(String type){

        //住宅
        if("1".equals(type)){

            leftListItems.add("公寓");
            leftListItems.add("電梯大樓");
            leftListItems.add("電梯大樓（飯店式管理）");
            leftListItems.add("豪宅");
            leftListItems.add("別墅");
            leftListItems.add("套房");
            leftListItems.add("四合院");
            leftListItems.add("平房");
            leftListItems.add("透天厝");
            leftListItems.add("樓中樓");
            leftListItems.add("其他");
            txtTitle.setText("住宅");
        }
        //店面
        else if("2".equals(type)){
            leftListItems.add("住店");
            leftListItems.add("賣場店面");
            leftListItems.add("三角窗店面");
            leftListItems.add("透天店面");
            leftListItems.add("市場攤位");
            leftListItems.add("辦公大樓配套店面");
            leftListItems.add("飯店配套店面");
            leftListItems.add("社區店面");
            leftListItems.add("觀光店面");
            leftListItems.add("商業街店面");
            leftListItems.add("其他");

            txtTitle.setText("店面");
        }
        //辦公室
        else if("3".equals(type)){
            leftListItems.add("純辦公大樓");
            leftListItems.add("住辦兩用");
            leftListItems.add("店住辦");
            leftListItems.add("工業住辦");
            leftListItems.add("其他");

            txtTitle.setText("辦公室");
        }
        //廠房
        else if("4".equals(type)){

            leftListItems.add("純厰房");
            leftListItems.add("厰房厰辦");
            leftListItems.add("倉庫");
            leftListItems.add("其他");
            txtTitle.setText("廠房");
        }
        //車位
        else if("5".equals(type)){
            leftListItems.add("平面");
            leftListItems.add("機械");
            txtTitle.setText("車位");

        }
        //土地
        else if("6".equals(type)){

            leftListItems.add("住宅用地");
            leftListItems.add("商業用地");
            leftListItems.add("工業用地");
            leftListItems.add("農地");
            leftListItems.add("農牧地");
            leftListItems.add("山林牧地");
            leftListItems.add("墓地");
            leftListItems.add("休閑用地");
            leftListItems.add("停車場用地");
            leftListItems.add("道路用地");
            leftListItems.add("畸零地");
            leftListItems.add("礦鹽地");
            leftListItems.add("養殖地");
            leftListItems.add("其他");
            txtTitle.setText("土地");
        }
        adapter.notifyDataSetChanged();

    }

    public class LeftAdapter extends BaseAdapter {


        private LayoutInflater mInflater;


        public LeftAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return leftListItems.size();
        }

        @Override
        public Object getItem(int position) {
            return leftListItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View myconvertView, ViewGroup parent) {

            View leftconvertView = mInflater.inflate(R.layout.published_second_item, null);
            final String obj = leftListItems.get(position);
            final TextView txtName = (TextView) leftconvertView
                    .findViewById(R.id.txt_publish_content);
            txtName.setText(obj);


            leftconvertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //出租
                    if("1".equals(propertytype)){
                        //住宅
                        if("1".equals(type)){
                            PublishedRentActivity.gotoActivity(PublishedSecondActivity.this,type,""+position);
                        }
                        else if("3".equals(type)||"4".equals(type)){
                            PublishedRentOfficeActivity.gotoActivity(PublishedSecondActivity.this,type,""+position);
                        }
                        //店面
                        else if("2".equals(type)){
                            PublishedRentShopAcitivity.gotoActivity(PublishedSecondActivity.this,""+position);
                        }
                        //車位
                        else if("5".equals(type)){
                            PublishedThirdActivity.gotoActivity(PublishedSecondActivity.this,"1",""+position);
                        }
                        //土地
                        else if("6".equals(type)){
                            PublishedRentLandAcitivity.gotoActivity(PublishedSecondActivity.this,""+position);
                        }
                    }
                    //出售
                    else if("2".equals(propertytype)){
                        //住宅
                        if("1".equals(type)||"3".equals(type)||"4".equals(type)){
                            PublishedActivity.gotoActivity(PublishedSecondActivity.this,type,""+fillData(position));
                        }
                        //店面
                        else if("2".equals(type)){
                            PublishedShopActivity.gotoActivity(PublishedSecondActivity.this,""+fillData(position));
                        }
                        //車位
                        else if("5".equals(type)){
                            PublishedThirdActivity.gotoActivity(PublishedSecondActivity.this,"2",""+position);
                        }
                        //土地
                        else if("6".equals(type)){
                            PublishedLandActivity.gotoActivity(PublishedSecondActivity.this,""+fillData(position));
                        }
                    }


                }
            });

            return leftconvertView;
        }



    }

    private int fillData(int position){

        int result = 0;
        if(position==0){
            result=4;
        }
        else if(position==1){
            result=2;
        }
        else if(position==2){
            result=16;
        }
        else if(position==3){
            result=1;
        }
        else if(position==4){
            result=6;
        }
        else if(position==5){
            result=11;
        }
        else if(position==6){
            result=18;
        }
        else if(position==7){
            result=13;
        }

        else if(position==8){
            result=5;
        }
        else if(position==9){
            result=14;
        }
        else if(position==10){
            result=15;
        }



        return result;
    }


}
